from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework import status
from django.db import DatabaseError
from rest_framework.response import Response

def exception_handler(exc, context):
    # 处理DRF本身异常
    response = drf_exception_handler(exc, context)

    if response is None:
        # response 为None说明DRF未处理到异常，需要判断是否是其他异常
        view = context['view']
        if isinstance(exc, ZeroDivisionError):
            print('[%s]: %s' % (view, exc))
            response = Response({'detail': '服务器内部错误'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)
        else:
            response = Response({'detail': '服务器内部错误'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)


    return response